package com.yzt.zhuju.dialog;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yzt.zhuju.R;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.dialog.adapter.DiamondRechargeAdapter;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.goods.RechargeBaseBean;
import com.yzt.zhuju.network.entity.goods.RechargeBean;
import com.yzt.zhuju.network.entity.goods.WeChatPayBean;
import com.yzt.zhuju.network.entity.goods.WeChatPaySignBean;
import com.yzt.zhuju.utils.recyclerutils.GridItemDecoration;
import com.yzt.zhuju.widget.tuiview.TUILinearLayout;
import com.yzt.zhuju.widget.tuiview.TUITextView;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 钻石充值页面
 */
public class DiamondRechargeDialog extends BottomBaseDialog {

    private DiamondRechargeAdapter diamondRechargeAdapter;

    private String productId;

    TUITextView tvBuy;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_diamond_recharge;
    }

    @Override
    public void bindView(View v) {

        boolean dark = getArguments().getBoolean("dark", false);

        tvBuy = findViewById(R.id.tv_next);

        TUILinearLayout llContainer = findViewById(R.id.ll_container);
        TUITextView tvTitle = findViewById(R.id.tv_title);
        llContainer.setSelected(dark);
        tvTitle.setSelected(dark);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        diamondRechargeAdapter = new DiamondRechargeAdapter();
        diamondRechargeAdapter.setDark(dark);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recyclerView.setAdapter(diamondRechargeAdapter);

        recyclerView.addItemDecoration(new GridItemDecoration.Builder()
                .divider(24)
                .build());

        diamondRechargeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                for (RechargeBean bean : diamondRechargeAdapter.getData()) {
                    bean.setCheck(false);
                }
                productId = diamondRechargeAdapter.getData().get(position).getPackId();
                diamondRechargeAdapter.getData().get(position).setCheck(true);
                diamondRechargeAdapter.notifyDataSetChanged();
                tvBuy.setText("确认支付（" + diamondRechargeAdapter.getData().get(position).getPrice() + ")");
            }
        });

        findViewById(R.id.tv_protocol).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterMain.goToWeb("http://h5.51zhuju.com/service.html");
            }
        });

        tvBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (productId == null) {
                    return;
                }
                createOrder();
            }
        });

        loadPageData();
    }

    /**
     * 获取页面数据
     */
    private void loadPageData() {
        BizLayer.getInstance().getMineModule().getPaymentList(new AresSubscriber<RechargeBaseBean>() {
            @Override
            protected void onSuccess(RechargeBaseBean list) {
                if (!list.getList().isEmpty()) {
                    productId = list.getList().get(0).getPackId();
                    tvBuy.setText("确认支付（" + list.getList().get(0).getPrice() + ")");
                    list.getList().get(0).setCheck(true);
                }
                diamondRechargeAdapter.addData(list.getList());
            }
        });
    }

    /**
     * 获取页面数据
     */
    private void createOrder() {
        BizLayer.getInstance().getMineModule().createPayOrder(productId, new AresSubscriber<WeChatPayBean>() {
            @Override
            protected void onSuccess(WeChatPayBean list) {
               // sendReq failed for wechat app signature check failed
                // 唤醒微信支付
                WeChatPaySignBean payForData = list.getWechatSign();
                IWXAPI api = WXAPIFactory.createWXAPI(mActivity, payForData.getAppid());
                PayReq request = new PayReq();
                request.appId = payForData.getAppid();
                request.partnerId = payForData.getPartnerId();
                request.prepayId = payForData.getPrepayId();
                request.packageValue = payForData.getPackageValue();
                request.nonceStr = payForData.getNoncestr();
                request.timeStamp = payForData.getTimestamp();
                request.sign = payForData.getSign();
                //定义额外数据
                Gson gson = new Gson();
//                WeChatPayBean bean = new WeChatPayBean();
//                WeChatPayBean.PayData jsonData = new WeChatPayBean.PayData();
//                jsonData.setSize(1);
//                jsonData.setTotalPrice(100);
//                jsonData.setUnitPrice(100);
//                bean.setTag("buy");
//                bean.setJson(gson.toJson(jsonData));
                //放置额外数据
//                request.extData = gson.toJson(bean);
                api.sendReq(request);
            }
        });
    }
}

